package com.campus.shop;


import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Collections;

/**
 * @Description:代码生成器
 * @Author: xr
 * @Date: 2023/4/24 16:21
 */
@SpringBootTest
public class CodeGenerator {

    @Test
    public void doGenerate(){
        String tableName = "tb_order_carts";
        generate(tableName);
    }

    private static void generate(String tableName){
        String projectPath = System.getProperty("user.dir");
        // 数据源配置
        DataSourceConfig.Builder dataSourceConfig = new DataSourceConfig
                .Builder(
                "jdbc:mysql://localhost:3306/campus_shop?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8",
                "root",
                "root")
                .dbQuery(new MySqlQuery())
                .typeConvert(new MySqlTypeConvert())
                .keyWordsHandler(new MySqlKeyWordsHandler());

        FastAutoGenerator.create(dataSourceConfig)
                .globalConfig(builder -> {
                    builder.author("xr") //设置作者
                            .commentDate("YYYY-MM-DD HH:mm:ss")//注释日期
                            .outputDir(projectPath + "/src/main/java/"); //指定输出目录

                })
                .packageConfig(builder -> {
                    builder.parent("com.campus.shop") // 设置父包名
                            .moduleName("") // 设置父包模块名
                            .entity("entity")
                            .service("service") // service包名
                            .serviceImpl("service.impl") // serviceImpl包名
                            .mapper("mapper") // mapper包名
                            .controller("controller") // Controller 包名	默认值:controller
                            .other("param") // 自定义文件包名	输出自定义文件时所用到的包名
                            .pathInfo(Collections.singletonMap(OutputFile.entity, projectPath + "\\src\\main\\java\\"))
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml,projectPath + "\\src\\main\\resources\\mapper\\")); // 设置mapperXml生成路径
                })

                //使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new VelocityTemplateEngine())
                //设置自定义模板路径
                .templateConfig(builder -> {
                    builder.entity("/templates/entity.java.vm")
                            .service("/templates/service.java.vm")
                            .serviceImpl("/templates/serviceImpl.java.vm")
                            .mapper("/templates/mapper.java.vm")
                            .controller("/templates/controller.java.vm");
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder().enableLombok();
                    //                    builder.mapperBuilder().enableMapperAnnotation().build();
                    builder.controllerBuilder().enableHyphenStyle()  // 开启驼峰转连字符
                            .enableRestStyle();  // 开启生成@RestController 控制器
                    builder.addInclude(tableName) // 设置需要生成的表名
                            .addTablePrefix("tb_", "sys_"); // 设置过滤表前缀
                    builder .controllerBuilder().enableRestStyle()//开启@RestController风格
                            .serviceBuilder().formatServiceFileName("%sService"); //去掉默认的I前缀

                })
                .execute();

    }









}
